/*
 * FileName：WebsiteBrandIntroductionServiceImpl.java 
 * <p>
 * Copyright (c) 2017-2020 <a href="https://www.smartwx.info">hermit(1154808491@qq.com)</a>.
 * <p>
 * Licensed under the GNU General Public License, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/gpl-3.0.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.smart.wx.wxcms.service.impl;

import javax.annotation.Resource;

import com.smart.common.util.AjaxResult;
import com.smart.common.util.StringUtil;
import com.smart.common.util.Util;
import com.smart.wx.wxcms.dao.WebsiteBrandIntroductionMapper;
import com.smart.wx.wxcms.entity.WebsiteBrandIntroduction;
import com.smart.wx.wxcms.service.WebsiteBrandIntroductionService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 网站模板-----品牌介绍
 *
 * @author hermit
 * @version 2.0
 * @date 2018-07-12 23:13:44
 */
@Service
public class WebsiteBrandIntroductionServiceImpl implements WebsiteBrandIntroductionService {

    @Resource
    private WebsiteBrandIntroductionMapper websiteBrandIntroductionMapper;

    @Override
    public WebsiteBrandIntroduction queryObject(WebsiteBrandIntroduction websiteBrandIntroduction) throws Exception {
        return websiteBrandIntroductionMapper.queryObject(websiteBrandIntroduction);
    }

    @Override
    public List<WebsiteBrandIntroduction> queryList(WebsiteBrandIntroduction websiteBrandIntroduction) throws Exception {
        return websiteBrandIntroductionMapper.queryList(websiteBrandIntroduction);
    }

    @Override
    public List<WebsiteBrandIntroduction> queryListByPage(WebsiteBrandIntroduction websiteBrandIntroduction) throws Exception {
        return websiteBrandIntroductionMapper.queryListByPage(websiteBrandIntroduction);
    }

    @Override
    public Integer queryCount(WebsiteBrandIntroduction websiteBrandIntroduction) throws Exception {
        Integer ct = websiteBrandIntroductionMapper.queryCount(websiteBrandIntroduction);
        return ct == null ? 0 : ct;
    }

    @Override
    public Integer insert(WebsiteBrandIntroduction websiteBrandIntroduction) throws Exception {
        return websiteBrandIntroductionMapper.insert(websiteBrandIntroduction);
    }

    @Override
    public Integer update(WebsiteBrandIntroduction websiteBrandIntroduction) throws Exception {
        return websiteBrandIntroductionMapper.update(websiteBrandIntroduction);
    }

    @Override
    public Integer delete(WebsiteBrandIntroduction websiteBrandIntroduction) throws Exception {

        String introductionId = websiteBrandIntroduction.getId();
        if (StringUtil.isNotBlank(introductionId)) {
            this.websiteBrandIntroductionMapper.deleteBatch(Arrays.asList(introductionId.split(",")));
        }
        return 1;
    }

    @Override
    public Integer deleteBatch(List<String> ids) throws Exception {
        return websiteBrandIntroductionMapper.deleteBatch(ids);
    }


    @Override
    public AjaxResult saveOrUpdate(WebsiteBrandIntroduction introduction) throws Exception {

        /************* STEP_1: 新增或修改************/
        String introductionId = introduction.getId();
        if (StringUtil.isNotBlank(introductionId)) { // 修改
            introduction.setUpdateTime(new Date());
            this.websiteBrandIntroductionMapper.update(introduction);
        } else { // 新增

            /************ STEP_2: 目前只允许存在一条记录 *****************/

            int count = this.websiteBrandIntroductionMapper.queryCount(introduction);
            if (count >= 1) {
                return AjaxResult.failure("禁止再次新增!");
            }

            introduction.setId(Util.getUUID());
            introduction.setCreateTime(new Date());
            introduction.setUpdateTime(new Date());
            this.websiteBrandIntroductionMapper.insert(introduction);
        }

        return AjaxResult.saveSuccess();
    }
}
